//
//  DownLoadViewController.m
//  Travel_M_Travel
//
//  Created by dlios on 15-4-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DownLoadViewController.h"
#import "PlayController.h"

@interface DownLoadViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation DownLoadViewController

+ (DownLoadViewController *)shareInstance
{
    static DownLoadViewController *downloadVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        downloadVC = [[DownLoadViewController alloc] init];
    });
    return downloadVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"已下载";
    self.view.backgroundColor = [UIColor whiteColor];
    //    self.arr = [NSMutableArray array];
    
    // 分段控制器
    //    NSArray *segmentedArray = [[NSArray alloc] initWithObjects:@"下载中", @"已下载", nil];
    //    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    //    self.segmentedControl.frame = CGRectMake(0, 64, self.view.frame.size.width, 30);
    //    [self.segmentedControl setTintColor:[UIColor whiteColor]];
    //    self.segmentedControl.backgroundColor = [UIColor blackColor];
    //    [_segmentedControl addTarget:self action:@selector(segmentAction:)
    //                forControlEvents:UIControlEventValueChanged];
    //    [self.segmentedControl setSelectedSegmentIndex:0];
    //    [self.view addSubview:_segmentedControl];
    //    [_segmentedControl release];
    
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"default"];
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.arr.count == 0) {
        return 1;
    } else {
        return self.arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arr.count == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
        cell.textLabel.text = @"暂无下载";
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"default"];
        Tracks *t = [self.arr objectAtIndex:indexPath.row];
        cell.textLabel.text = t.title;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Tracks *t = [self.arr objectAtIndex:indexPath.row];
    if (t.isFinish) {
        PlayController *playVC = [PlayController shareInstance];
        playVC.track = t;
        [self.navigationController pushViewController:playVC animated:YES];
    } else {
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载中, 请稍后..." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [al show];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
